package com.boyadegroot.divelog.models;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

public class DiveSample implements Parcelable, Comparable<DiveSample> {

    public enum Bookmark {
        NOBOOKMARK(0, 0, "No bookmark"),
        DIVEACTIVE(1, 1, "Dive active"),
        SURFACED(2, 125, "Surfaced"),
        ASCENTWARNING(3, 120, "Ascent warning"),
        RGBMWARNING(4, 123, "RGBM warning"),
        MANDATORYSAFETYSTOP(5, 121, "Mandatory safety stop"),
        MANDATORYSAFETYSTOPCEILINGERROR(6, 127, "Mandatory safety stop ceiling error"),
        PERSONAL(99, 150, "Personal"),
        OTHER(100, 124, "Other");
        private int value;
        private int code;
        private String string;
        private Bookmark(int value, int code, String string) {
            this.value = value;
            this.code = code;
            this.string = string;
        }
        public static Bookmark fromInt(int i) {
            for (Bookmark bookmark : Bookmark.values()) {
                if (bookmark.getValue() == i) { return bookmark; }
            }
            return null;
        }
        public static Bookmark fromCode(int code) {
            for (Bookmark bookmark : Bookmark.values()) {
                if (bookmark.getCode() == code) { return bookmark; }
            }
            return OTHER;
        }
        public int getValue() {
            return this.value;
        }
        public int getCode() {
            return this.code;
        }
        @Override
        public String toString() {
            return string;
        }
    }

    private static final String TAG = DiveSample.class.getName();

    private long mId;
    private long mDiveId; // do not store the Dive to maintain the class lightweight
    private int mSampleTime;
    private double mDepth;
    private int mPressure;
    private int mTemperature;
    private Bookmark mBookmark = Bookmark.NOBOOKMARK;
    private String mBookmarkText;
    private double mSacRate;
    private int mCylinderPressure;

    public DiveSample() {}

    public DiveSample(
            long id,
            long diveId,
            int sampleTime,
            double depth,
            int pressure,
            int temperature,
            Bookmark bookmark,
            String bookmarkText,
            double sacRate,
            int cylinderPressure) {
        this.setId(id);
        this.setDiveId(diveId);
        this.setSampleTime(sampleTime);
        this.setDepth(depth);
        this.setPressure(pressure);
        this.setTemperature(temperature);
        this.setBookmark(bookmark);
        this.setSacRate(sacRate);
        this.setCylinderPressure(cylinderPressure);
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        this.mId = id;
    }

    public long getDiveId() {
        return mDiveId;
    }

    public void setDiveId(long diveId) {
        this.mDiveId = diveId;
    }
    
    public int getSampleTime() {
        return mSampleTime;
    }

    public void setSampleTime(int sampleTime) {
        this.mSampleTime = sampleTime;
    }
    
    public double getDepth() {
        return mDepth;
    }

    public void setDepth(double depth) {
        this.mDepth = depth;
    }
    
    public int getPressure() {
        return mPressure;
    }

    public void setPressure(int pressure) {
        this.mPressure = pressure;
    }
    
    public int getTemperature() {
        return mTemperature;
    }

    public void setTemperature(int temperature) {
        this.mTemperature = temperature;
    }
    
    public Bookmark getBookmark() {
        return mBookmark;
    }

    public void setBookmark(Bookmark bookmark) {
        this.mBookmark = bookmark;
    }
    
    public String getBookmarkText() {
        return mBookmarkText;
    }

    public double getSacRate() {
        return mSacRate;
    }

    public void setBookmarkText(String bookmarkText) {
        this.mBookmarkText = bookmarkText;
    }

    public void setSacRate(double sacRate) {
        this.mSacRate = sacRate;
    }
    
    public int getCylinderPressure() {
        return mCylinderPressure;
    }

    public void setCylinderPressure(int cylinderPressure) {
        this.mCylinderPressure = cylinderPressure;
    }

    @Override
    public int compareTo(@NonNull DiveSample another) {
        return ((Integer) this.mSampleTime).compareTo(another.mSampleTime);
    }

    @Override
    public String toString() {
        return Integer.toString(getSampleTime());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(mId);
        dest.writeLong(mDiveId);
        dest.writeInt(mSampleTime);
        dest.writeDouble(mDepth);
        dest.writeInt(mPressure);
        dest.writeInt(mTemperature);
        dest.writeInt(mBookmark.getValue());
        dest.writeDouble(mSacRate);
        dest.writeInt(mCylinderPressure);
    }

    public static final Parcelable.Creator<DiveSample> CREATOR = new Parcelable.Creator<DiveSample>() {

        public DiveSample createFromParcel(Parcel in) {
            return new DiveSample(in);
        }

        public DiveSample[] newArray(int size) {
            return new DiveSample[size];
        }
    };

    private DiveSample(Parcel in) {
        mId = in.readLong();
        mDiveId = in.readLong();
        mSampleTime = in.readInt();
        mDepth = in.readDouble();
        mPressure = in.readInt();
        mTemperature = in.readInt();
        mBookmark = Bookmark.fromInt(in.readInt());
        mSacRate = in.readDouble();
        mCylinderPressure = in.readInt();
    }
}
